<template>
  <div class="goods_list">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    
    <el-card>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input clearable @clear="initGoodsList" v-model="queryCascade.query" placeholder="请输入内容" >
            <el-button slot="append" icon="el-icon-search" @click="initGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="jumpToAddGoods">添加商品</el-button>
        </el-col>
      </el-row>

      <el-table :data="goods_list" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)"  width="130px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100px"></el-table-column>
        <el-table-column label="创建时间" width="180px">
          <template slot-scope="scope">{{scope.row.add_time|dateFmt}}</template>
        </el-table-column>
        <el-table-column label="操作"  width="160px">
          <template slot-scope="scope">
            <el-button type="primary" class="el-icon-edit" size="mini"></el-button>
            <el-button type="danger" class="el-icon-delete" size="mini" @click="delGoods(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryCascade.pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="queryCascade.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default{
  data(){
    return {
      goods_list:[],
      queryCascade:{
        query:'',
        pagenum:1,
        pagesize:5
      },
      total:0
    }
  },
  created(){
    this.initGoodsList()
  },
  methods:{
    async initGoodsList(){
      const res= await this.$http.get('goods',{params:this.queryCascade})
      console.log(res);
      if(res.data.meta.status!==200) return this.$message.error('获取数据失败')
      this.goods_list=res.data.data.goods
      this.total=res.data.data.total
    },
    async delGoods(id){
      const flag=await this.$confirm('是否确定删除商品','提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).catch(err=>{
        return  err
      })
      if(flag=='confirm'){
        const res= await this.$http.delete('goods/'+id)
        console.log(res); 
        if(res.data.meta.status!==200) this.$message.error('删除失败')
        this.$message.success('删除成功')
        this.initGoodsList()
      }
    },
    handleSizeChange(newVal){
      console.log(newVal);
      this.queryCascade.pagesize=newVal
      this.initGoodsList()
    },
    handleCurrentChange(newVal){
      console.log(newVal);
      this.queryCascade.pagenum=newVal
      this.initGoodsList()
    },
    jumpToAddGoods(){
      this.$router.push('/goods/add_goods')
    }
  }
}
</script>

<style lang="less" scoped>
.goods_list{
  .el-card{
    margin-top: 20px;
    .el-table{
      margin-top: 20px;
    }
    .el-pagination{
      margin-top: 20px;
    }
  }
}
</style>